package com.example.floristshop.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.floristshop.R;
import com.example.floristshop.database.Comment;
import com.example.floristshop.database.Flower;
import com.example.floristshop.database.User;
import com.example.floristshop.utils.DBFindUtil;
import com.example.floristshop.utils.TimeUtil;
import com.example.floristshop.utils.UserImageUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {

    private List<Comment> comment_list;
    private int comment_item;

    public CommentAdapter(AppCompatActivity activity, int _comment_item, List<Comment> _comment_list){
        this.comment_list = _comment_list;
        this.comment_item = _comment_item;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View commentView;
        CircleImageView user_image_civ;
        TextView user_name_tv, comment_text_tv, comment_time_tv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            commentView = itemView;
            user_image_civ = (CircleImageView)itemView.findViewById(R.id.comment_user_image);
            user_name_tv = (TextView)itemView.findViewById(R.id.comment_user_name);
            comment_text_tv = (TextView)itemView.findViewById(R.id.comment_text);
            comment_time_tv = (TextView)itemView.findViewById(R.id.comment_time);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(
                comment_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Comment comment = comment_list.get(position);
        User user = DBFindUtil.findUserByUserId(comment.getUser_id());
        holder.user_image_civ.setImageResource(UserImageUtil.id_to_src(user.getImage_id()));
        holder.user_name_tv.setText(user.getName());
        holder.comment_text_tv.setText(comment.getText());
        holder.comment_time_tv.setText(TimeUtil.timestamp_to_timeString(comment.getTimestamp()));
    }

    @Override
    public int getItemCount() {
        return comment_list.size();
    }
}
